/*!
// Testweb v0.4.0
// http://or-change.cn
// Copyright 2014 OrChange Inc. All rights reserved.
// Licensed under the GPL License.
*/
/*jslint vars: true, forin: true, plusplus: true */
/*global define: false */
/**
 * Dict tool functions. Factory function.
 * @module dictor
 * @requires randexp
 * @example
 *     var Dict = require("./oc.dict"); //How to require Dict module.
 */
define(function (require, exports, module) {
	'use strict';
	require("lib/core/randexp");
	var Dictionary = require("lib/class/dictionary");
	var dictor;

	/**
	 * Dict tool functions. Factory function.
	 * @class Dictor.
	 * @final
	 */
	module.exports = dictor = {
		/**
		 * Storage all dictionary.这些字典都是Dictionary的合法实例。
		 * @property dictionary_set
		 * @type array
		 */
		dictionary_set: [],
		/**
		 * Point to a single dictor in data
		 * @property CURRENT
		 * @type Dictor
		 * @default null
		 */
		CURRENT: null
	};
	/**
	 * 打开一本已经存在的字典，也可以理解为装载一个字典实例
	 *
	 * @method openDictionary
	 * @param dictionary {Dictionary}
	 * @returns this {dictor}
	 * @example
	 *			dictor.openDictionary([Dictionary]);
	 */
	dictor.openDictionary = function (dictionary) {
		this.dictionary_set.push(this.CURRENT = dictionary);

		return this;
	};
	/**
	 * 用模式配置方法随机生成一个字典
	 * @method buildInArray
	 * @param keys {object}
	 * @param amount {number}
	 * @param name {string}
	 * @returns {object}
	 * @example
	 *		dictor.createDictionaryByPattern({
	 *			name: "Some Words",//必要的
	 *			keys: {
	 *				"username":/zjm\d{6}/,
	 *				"password":/\d{8}/,
	 *				"email":/[a-z0-9]{3,10}@[a-z]{3,9}(\.[a-z]{2,4}){1,2}/
	 *			},//必要的
	 *			length: 2000,//可选的
	 *			comment: "More words!"//可选的
	 *		});
	 */
	dictor.createDictionaryByPattern = function (options) {
		var column, key, pattern, i, data = {},
			length = options.length,
			keys = options.keys;

		for (key in keys) {
			column = data[key] = [];
			pattern = keys[key];
			for (i = 0; i < length; i++) {
				column.push(pattern.gen);
			}
		}

		this.openDictionary(new Dictionary({
			data: data,
			name: options.name || "Warning: No Name, alert by dictor!",
			comment: options.comment || ""
		}));

		return this;
	};
	/**
	 * 用模式配置方法随机生成一个字典
	 * @method buildInArray
	 * @param keys {object}
	 * @param amount {number}
	 * @param name {string}
	 * @returns {object}
	 * @example
	 *		dictor.createDictionaryByPattern({
	 *			keys: ["key1","key2","key3"],//必要的
	 *			name: "Some Words",//必要的
	 *			comment: "More words!"//可选的
	 *		});
	 */
	dictor.createDictionary = function (options) {
		var key, data = {},
			keys = options.keys;

		// 生成空列数据集
		while (keys.length) {
			data[keys.pop()] = [];
		}

		this.openDictionary(new Dictionary({
			data: data,
			name: options.name || "Warning: No Name, alert by dictor!",
			comment: options.comment || ""
		}));

		return this;
	};
	/**
	 * Append one row to Dictionary.
	 * @method appendRow
	 * @param row {object}
	 * @returns this {Dictor}
	 * @example
	 *		dictor.appendRow({
	 *			key1: value1,
	 *			key2: value2,
	 *			key3: value3,
	 *			...
	 *		});
	 */
	dictor.appendRow = function (row) {
		var key, data = this.CURRENT.getProperty("data");

		for (key in data) {
			if (row.hasOwnProperty(key)) {
				data[key].push(row[key]);
			} else {
				data[key].push("");
			}
		}

		return this;
	};
	/**
	 * Remove one or more row from Dictionary by index.
	 * @method removeRow
	 * @param index {array}
	 * @returns this {Dictor}
	 * @example
	 *		dictor.removeRows([0,4,7,11,..]);
	 *		dictor.removeRows([8]);
	 */
	dictor.removeRows = function (index) {
		var i, key, data = this.CURRENT.getProperty("data");

		while (index.length) {
			i = index.pop();
			for (key in data) {
				data[key].splice(i, 1);
			}
		}

		return this;
	};
	/**
	 * Modify a row from Dictionary.
	 * @method modifyValue
	 * @param key {string}
	 * @param index {number}
	 * @param value {string}
	 * @returns {object}
	 * @example
	 *		dictor.modifyValue("username", 4, "william stone");
	 */
	dictor.modifyValue = function (key, index, value) {
		var data = this.CURRENT.getProperty("data");
		data[key][index] = value;

		return this;
	};
	/**
	 * 将CURRENT指针根据一个合法的index指向一个dictionary_set中的一个字典实例
	 * @method modifyValue
	 * @param key {string}
	 * @param index {number}
	 * @param value {string}
	 * @returns {object}
	 * @example
	 *		dictor.modifyValue("username", 4, "william stone");
	 */
	dictor.checkout = function (index) {
		this.CURRENT = this.dictionary_set[index];

		return this;
	};
	/**
	 * Output the list of dictionary
	 * @method getDictionaryList
	 * @param key {number}
	 * @returns {array} dictionary_list
	 */
	//TODO 增加pos和limit参数为翻页做准备
	dictor.getDictionaryList = function () {
		var i, len, dictionary_list = [];
		len = this.dictionary_set.length;
		for (i = 0; i < len; i += 1) {
			dictionary_list.push({
				dictor_name: this.dictionary_set[i].getInfo("name")
			});
		}
		return dictionary_list;
	};

	dictor.getDictionary = function (index) {
		return this.dictionary_set[index];
	};

	window.dictor = dictor;
});